﻿using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;

/// <summary>
/// 简单跑马灯，目前只支持竖向的滚动
/// </summary>
public class UIHorseRaceLamp : MonoBehaviour
{
    private int index = 0;
    public float height = 60;
    public float transitionTime = 1f;
    public float displayTime = 1f;
    // Start is called before the first frame update

    private void Start()
    {
        this.ResetState();
    }

    private void OnEnable()
    {
        StartCoroutine(RunRace());
    }

    private void ResetState()
    {
        for (int i = 0; i < this.transform.childCount; i++)
        {
            this.transform.GetChild(i).transform.localPosition = new Vector3(0, height * i, 0);
        }
    }

    private void OnDisable()
    {
        StopAllCoroutines();
    }

    public IEnumerator RunRace()
    {
        while (true)
        {
            int curIndex = (index) % this.transform.childCount;
            int nextIndex = (index - 1) % this.transform.childCount;

            for (int i = 0; i < this.transform.childCount; i++)
            {
                if (i == curIndex)
                {
                    this.transform.GetChild(i).transform.DOLocalMove(new Vector3(0, height * 0, 0), transitionTime);
                }
                else if (i == nextIndex)
                {
                    this.transform.GetChild(i).transform.DOLocalMove(new Vector3(0, height * 1, 0), transitionTime);
                }
                else
                {
                    this.transform.GetChild(i).localPosition = new Vector3(0, height * -1, 0);
                }
            }


            // yield return new WaitForSeconds(transitionTime);
            yield return new WaitForSeconds(displayTime);
            index++;
        }

    }


}
